/*! ******************************************************************************
 *
 * Pentaho Data Integration
 *
 * Copyright (C) 2002-2017 by Hitachi Vantara : http://www.pentaho.com
 *
 *******************************************************************************
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with
 * the License. You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 ******************************************************************************/

package org.yzbdl.lanius.ui.spoon.market.dialog;

import java.util.List;

import org.eclipse.swt.SWT;
import org.eclipse.swt.browser.LaniusBrowser;
import org.eclipse.swt.layout.FormData;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Dialog;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.Shell;
import org.pentaho.di.ui.core.PropsUI;
import org.pentaho.di.ui.core.database.dialog.DatabaseDialog;
import org.pentaho.di.ui.core.gui.GUIResource;

import com.google.common.collect.Lists;

/**
 *
 * 二维码支付对话框
 *
 * @author hs@yzbdl.ac.cn
 *
 */
public class QrCodePaymentDialog extends Dialog {
	private static Class<?> PKG = DatabaseDialog.class; // for i18n purposes, needed by Translator2!!

	private String dialogTitle = "扫码支付";
	private List<String> pluginVersionIds = Lists.newArrayList();

	private Button wOK;
	private FormData fdOK;
	private Listener lsOK;

	private LaniusBrowser wBrowser;
	private FormData fdBrowser;

	private Shell shell;
	private PropsUI props;

	private int buttonHeight = 30;

	private int flagStatus = 0;
	private Runnable showTime;

	public QrCodePaymentDialog(Shell parent, String pluginVersionId) {
		super(parent, SWT.NONE);
		props = PropsUI.getInstance();
		this.pluginVersionIds.add(pluginVersionId);
	}

	/**
	 * @param shell2
	 * @param versionIds
	 */
	public QrCodePaymentDialog(Shell parent, List<String> pluginVersionIds) {
		super(parent, SWT.NONE);
		props = PropsUI.getInstance();
		this.pluginVersionIds.addAll(pluginVersionIds);
	}

	public void open() {
		Shell parent = getParent();
		Display display = parent.getDisplay();

		shell = new Shell(parent, SWT.RESIZE | SWT.MAX | SWT.MIN);
		shell.setImage(GUIResource.getInstance().getImageSpoon());
		props.setLook(shell);

		shell.open();
		final int time = 3000;

		showTime = new Runnable() {
			public void run() {
				// 调用js方法
				wBrowser.execute("checkMsg()");
				display.timerExec(time, this);
			}
		};
		display.timerExec(time, showTime);
		while (flagStatus != 0) {
			dispose();
		}
		while (!shell.isDisposed()) {
			if (!display.readAndDispatch()) {
				display.sleep();
			}
		}

	}

	public void dispose() {
		shell.dispose();
	}

	/**
	 * 关闭检查是否支付线程
	 */
	public void coloseCheckPayOrder() {
		getParent().getDisplay().timerExec(-1, showTime);
	}

	private void ok() {
		dispose();
	}

}
